package com.ucw.mountain.util;

import java.util.Calendar;

public class StringUtil {

    public static boolean isBlank(String val) {
        if (val == null || "".equals(val)) {
            return true;
        }

        return false;
    }

    public static boolean contains(String src, String dest) {
        if (isBlank(src) || isBlank(dest)) {
            return false;
        }

        if (src.indexOf(dest) == -1) {
            return false;
        }

        return true;
    }

    public static boolean endWith(String src, String dest) {
        if (isBlank(src) || isBlank(dest)) {
            return false;
        }

        if (src.endsWith(dest)) {
            return true;
        }

        return false;
    }

    public static boolean matches(String src, String regex) {
        if (isBlank(src) || isBlank(regex)) {
            return false;
        }

        return src.matches(regex);
    }

    public static String replace(String srcStr, String[] replaceStr, char startReplaceChar, char endReplaceChar) {
        if (srcStr == null || replaceStr == null) {
            return null;
        }
        StringBuilder sb = new StringBuilder();
        char nowC;
        char nextC;
        int len = srcStr.length();
        for (int i = 0; i < len; i++) {
            nowC = srcStr.charAt(i);
            if (nowC == startReplaceChar) {
                if ((i + 2) < len) {
                    if (srcStr.charAt((i + 2)) == endReplaceChar) {
                        nextC = srcStr.charAt(i + 1);
                        if (Character.isDigit(nextC)) {
                            int nextI = Integer.parseInt("" + nextC);
                            if (nextI < replaceStr.length) {
                                i += 2;
                                sb.append(replaceStr[nextI]);
                                continue;
                            }
                        }
                    }
                }
            }

            sb.append(nowC);
        }

        return sb.toString();
    }

    public static String replace(String srcStr, String... replaceStr) {
        return replace(srcStr, replaceStr, '{', '}');
    }

    public static String substring(String srcStr, int sublen) {
        if (isBlank(srcStr)) {
            return "";
        }

        if (srcStr.length() <= sublen) {
            return srcStr;
        }

        return srcStr.substring(0, sublen);
    }

    public static String trimToEmpty(String val) {
        if (isBlank(val)) {
            return "";
        }

        return val.trim();
    }

    public static String notBlankAddSpace(String val) {
        if (isTrimBlank(val)) {
            return "";
        }

        return val + " ";
    }

    public static boolean isTrimBlank(String val) {
        if (val == null || "".equals(val.trim())) {
            return true;
        }

        return false;
    }

    public static boolean isInteger(String data) {
        try {
            new Integer(data);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    public static String getObject2StrWithName(String name, Object o) {
        String var = name + ":" + JsonUtil.getJsonFromObject(o);
        return var;
    }

    public static String args2Str(String... args) {
        String var = "[";
        for (String t : args) {
            var += t + ",";
        }
        var = var.substring(0, var.lastIndexOf(","));
        var += "]";
        return var;
    }

    public static String nullAsEmpty(String str) {
        return str == null ? "" : str;
    }

    /**
     * 检查对象是否为数字型字符串,包含负数开头的。
     */
    public static boolean isNumeric(Object obj) {
        if (obj == null) {
            return false;
        }
        char[] chars = obj.toString().toCharArray();
        int length = chars.length;
        if (length < 1)
            return false;

        int i = 0;
        if (length > 1 && chars[0] == '-')
            i = 1;

        for (; i < length; i++) {
            if (!Character.isDigit(chars[i])) {
                return false;
            }
        }
        return true;
    }

    /**
     * 时间戳对应当天的小时
     *
     * @param time
     * @return
     */
    public static int getHourOfDayByTimeSecondes(Long time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(time * 1000);
        return calendar.get(Calendar.HOUR_OF_DAY);
    }

    /**
     * 前缀+0, 总长度为leng
     * 
     * @param targetValue
     * @return
     */
    public static String addPrefixZero(String targetValue, int targetLength) {
        synchronized (targetValue) {
            int len = targetValue.length();// 取得字符串的长度
            int needIndex = targetLength - len;
            for (int i = 0; i < needIndex; i++) {
                targetValue = "0" + targetValue;
            }
            return targetValue;
        }
    }

    /**
     * 去掉前缀为0
     * 
     * @param targetValue
     * @return
     */
    public static String removePrefixZero(String targetValue) {
        int len = targetValue.length();// 取得字符串的长度
        int index = 0;// 预定义第一个非零字符串的位置

        char strs[] = targetValue.toCharArray();// 将字符串转化成字符数组
        for (int i = 0; i < len; i++) {
            if ('0' != strs[i]) {
                index = i;// 找到非零字符串并跳出
                break;
            }
        }
        String strLast = targetValue.substring(index, len);// 截取字符串
        return strLast;
    }
}
